import json


from cross_chain_data.code_new.code_scan.bnbScan import get_bnbScan_data
from cross_chain_data.code_new.code_scan.gaiaScan import get_gaiaScan_data

from cross_chain_data.code_new.get_cross_chain_event import get_cross_chain_event
from cross_chain_data.code_new.get_cctx_xulie_eth_bsc import get_cctx_xulie_event_eth_bsc_source,get_cctx_xulie_event_eth_bsc_target
from cross_chain_data.code_new.get_cctx_xulie_ltc_doge_btc import get_cctx_xulie_event_ltc_doge_btc_source,get_cctx_xulie_event_ltc_doge_btc_target
from cross_chain_data.code_new.get_cctx_xulie_avax import get_cctx_xulie_event_avax_source,get_cctx_xulie_event_avax_target
from cross_chain_data.code_new.get_cctx_xulie_bch import get_cctx_xulie_event_bch_source,get_cctx_xulie_event_bch_target
f=open('../data/total_data.json','r')
content=f.read()
f.close()
total_data=json.loads(content)

print(type(total_data))

print(len(total_data))

#抽取交易序列数据
# 遍历总的数据
print("******************交易序列数据开始抽取******************")
# 使用字典进行动态的选择调用的函数
chain_function_xulie_source={
    'AVAX':get_cctx_xulie_event_avax_source,
    'BTC':get_cctx_xulie_event_ltc_doge_btc_source,
    'LTC':get_cctx_xulie_event_ltc_doge_btc_source,
    'ETH':get_cctx_xulie_event_eth_bsc_source,
    'BNB':get_bnbScan_data,
    'BCH':get_cctx_xulie_event_bch_source,
    'BSC':get_cctx_xulie_event_eth_bsc_source,
    'DOGE':get_cctx_xulie_event_ltc_doge_btc_source,
    'GAIA':get_gaiaScan_data
}
chain_function_xulie_target={
    'AVAX':get_cctx_xulie_event_avax_target,
    'BTC':get_cctx_xulie_event_ltc_doge_btc_target,
    'LTC':get_cctx_xulie_event_ltc_doge_btc_target,
    'ETH':get_cctx_xulie_event_eth_bsc_target,
    'BNB':get_bnbScan_data,
    'BCH':get_cctx_xulie_event_bch_target,
    'BSC':get_cctx_xulie_event_eth_bsc_target,
    'DOGE':get_cctx_xulie_event_ltc_doge_btc_target,
    'GAIA':get_gaiaScan_data
}
# 抽取的序列数据
data_xulie_cctx=[]
for data_cctx_dict in total_data:
    # 设置单条字典数据
    data_xulie={}
    # 获取源链目标链的名字
    source_chain_name = data_cctx_dict["cross_chain_data"]["input"]["chain"]
    target_chain_name = data_cctx_dict["cross_chain_data"]["output"]["chain"]

    # if source_chain_name in chain_function_xulie_source and target_chain_name in chain_function_xulie_source:
    if source_chain_name in ["ETH","BSC","LTC","BTC","DOGE","AVAX","BCH"] and target_chain_name in ["ETH","BSC","LTC","BTC","DOGE","AVAX","BCH"]:
        # 1.抽取源链事件
        Lock_event,Deposit_event=chain_function_xulie_source[source_chain_name](data_cctx_dict)
        # 2.抽取跨链平台事件
        Lock_action, Unlock_action=get_cross_chain_event(data_cctx_dict)
        # 3.抽取目标链事件
        Unlock_event=chain_function_xulie_target[target_chain_name](data_cctx_dict)

        # 4.添加数据到空字典中，并打标签
        data_xulie["Lock_event"]=Lock_event
        data_xulie["Deposit_event"]=Deposit_event
        data_xulie["Lock_action"]=Lock_action
        data_xulie["Unlock_action"]=Unlock_action
        data_xulie["Unlock_event"]=Unlock_event
        data_xulie["Type"]="1"

        data_xulie_cctx.append(data_xulie)
    else:
        print("源链或者目标链的平台数据目前无法获取！")

# 将最终的数据写入文件中
print(f"全部数据抽取完成，最终总共{len(data_xulie_cctx)}条数据")
json_data=json.dumps(data_xulie_cctx,indent=4)
with open('../data_new/total_data_xulie.json', 'w', encoding='utf-8') as f:
    f.write(json_data)